package net.easyjava.test.jingjuyuejujingdian;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.db.mysql.MysqlClient;
import net.easyjava.tools.http.HttpTools;

import java.sql.SQLException;

/**
 * @Author ludi
 * @Date 2016 06 29 下午2:26
 */
public class GetData {

	public static final String  jdbcUrl = "jdbc:mysql://mingjuer.mysql.rds.aliyuncs.com:3306/crawler";
	public static final String  jdbcName = "juer";
	public static final String  jdbcPassword = "Juer2016";

	//首页URL 展示所有分类 无翻页
	public static final String URL_ROOT = "http://app.9nali.com/index/460";
	//某分类下的专辑 %s 分类ID page_id=1,目前都只有一页
	public static final String URL_ALBUM = "http://app.9nali.com/460/bozhus/%s?page_id=1";
	//某专辑下的所有曲目 %s 专辑ID page_id专辑曲目最多的,41页。
	public static final String URL_ALBUM_AUDIO = "http://app.9nali.com/460/albums/%s?page_id=%s";


	public static void get() {
		MysqlClient client = null;
		try {
			client = MysqlClient.getInstance(jdbcUrl, jdbcName, jdbcPassword);
			String s = HttpTools.get(URL_ROOT);
			JSONObject jsonObject = JSONObject.parseObject(s);
			JSONArray categorys = jsonObject.getJSONArray("list");
			for (Object o : categorys) {
				JSONObject category = (JSONObject) o;
				CategoryBean categoryBean = JSONObject.parseObject(category.toString(), CategoryBean.class);
				System.out.println("保存-----:"+ categoryBean);
				saveCategory(client, categoryBean);
				String albumUrl = String.format(URL_ALBUM,category.get("uid"));
				JSONObject jsonObject1 = JSONObject.parseObject(HttpTools.get(albumUrl));
				JSONArray albums = jsonObject1.getJSONArray("list");
				for (Object o2 : albums) {
					JSONObject album = (JSONObject) o2;
					album.put("categoryId", category.get("uid"));
					AlbumBean albumBean = JSONObject.parseObject(album.toString(), AlbumBean.class);
					System.out.println("保存-----:"+ albumBean);
					saveAlbum(client, albumBean);
					for (int i=1;i<42;i++) {
						String audioUrl = String.format(URL_ALBUM_AUDIO, album.get("albumId"),i);
						JSONObject jsonObject2 = JSONObject.parseObject(HttpTools.get(audioUrl));
						JSONObject tracks = jsonObject2.getJSONObject("tracks");
						JSONArray audios = tracks.getJSONArray("list");
						if (audios == null) {
							continue;
						}
						for (Object o3 : audios) {
							JSONObject audio = (JSONObject) o3;
							AudioBean audioBean = JSONObject.parseObject(audio.toString(), AudioBean.class);
							System.out.println("保存-----:"+ audioBean);
							saveAudio(client, audioBean);
						}
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.destroy();
		}
	}

	private static void saveAudio(MysqlClient client, AudioBean audioBean) {
		String sql = "INSERT INTO JJYJJD_AUDIO(id,title,duration,audio_url_32,audio_url_64,img_url_small,img_url_large,mp3_size_32,mp3_size_64,album_id,category_id) " +
				" VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			client.execute(sql, audioBean.getId(), audioBean.getTitle(), audioBean.getDuration(), audioBean.getAudioUrl32(), audioBean.getAudioUrl64(),
                    audioBean.getImgUrlSmall(), audioBean.getImgUrlLarge(), audioBean.getMp3Size32(), audioBean.getMp3Size64(), audioBean.getAlbumId(), audioBean.getCategoryId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private static void saveAlbum(MysqlClient client, AlbumBean albumBean) {
		String sql = "INSERT INTO JJYJJD_ALBUM(id,title,front_url,total,category_id) VALUES(?,?,?,?,?)";
		try {
			client.execute(sql, albumBean.getId(), albumBean.getTitle(), albumBean.getFrontUrl(), albumBean.getTotal(), albumBean.getCategoryId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private static void saveCategory(MysqlClient client, CategoryBean categoryBean) {
		String sql = "INSERT INTO JJYJJD_CATEGORY(id,name,front_url,description,albums) VALUES(?,?,?,?,?)";
		try {
			client.execute(sql, categoryBean.getId(), categoryBean.getName(), categoryBean.getFrontUrl(), categoryBean.getDescription(), categoryBean.getAlbums());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
